package com.gitee.liuzhi.demo.juc;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @Author liuzhi
 * @Date 2023/9/6 20:43
 * @Version 1.0
 * @Description CountDownLatchTest
 */
@Slf4j
public class CountDownLatchDemo {

    private static final CountDownLatch COUNT_DOWN_LATCH = new CountDownLatch(10);

    private static final Integer THREAD_COUNT = 10;

    public static void main(String[] args) throws Exception {

        for (int i = 0; i < THREAD_COUNT; i++) {
            new Thread(() -> {
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    log.error(String.format("thread interrupted. message:%s",e.getMessage()),e);
                }
                System.out.println(Thread.currentThread().getName());
                COUNT_DOWN_LATCH.countDown();
            }
            ).start();
        }
        COUNT_DOWN_LATCH.await();
        System.out.println(Thread.currentThread().getName());
    }


}
